import cv2
from pyzbar.pyzbar import decode
import time

t1 = time.time()

# 读取图像
#image = cv2.imread("D:\\origin car\\opencv\\qrcode\\11111.jpg")
image = cv2.imread("/root/Pictures/qrcode)images/11111.jpg")
#image = cv2.imread("D:\\origin car\\opencv\\qrcode\\66666.jpg")

image = cv2.resize(image, (160, 120)) #（width, height）

print(image.shape) #（height, width, channel）

# 检查图像是否成功加载
if image is None:
    print("Error: Unable to load image. Please check the file path.")
else:
    # 解析二维码（直接解析彩色图像，提高效率）
    decoded_objects = decode(image) #返回一个列表，列表中的每个元素是一个对象
    #print(decoded_objects)
    # if decoded_objects:
    #     for obj in decoded_objects:
    #         qr_code_data = obj.data.decode('utf-8') #从字节码转换为字符串
    #         number = int(qr_code_data)
    #         #number = int(obj.data.decode('utf-8'))
    #         # #print(f"Detected QR code: {qr_code_data}")
    #         # #print(type(qr_code_data))
    #         sign_data = 3 if number % 2 == 1 else 4
    #         print(f"Detected QR code: Sign Data: {sign_data}")
    #     else:
    #         print(f"Non-numeric QR code detected")
    # else:
    #     print("No QR code detected.")

    qrcode = decoded_objects[0].data.decode('utf-8')
    number = int(qrcode)
    sign_data = 3 if number % 2 == 1 else 4
    print(f"Detected QR code: Sign Data: {sign_data}")
    print (qrcode)

t2 = time.time()
print(f"Execution Time: {t2 - t1:.6f} seconds")
